<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="com.gzsz.util.DBUtil" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    String patient_name = request.getParameter("patient_name");
    String age = request.getParameter("age");
    String gender = request.getParameter("gender");
    String birthday = request.getParameter("birthday");
    String diagnosis_result = request.getParameter("diagnosis_result");
    String admission_date = request.getParameter("admission_date");
    String expected_discharge_date = request.getParameter("expected_discharge_date");
    String ward_number = request.getParameter("ward_number");
    String bed_number = request.getParameter("bed_number");

    String id = request.getParameter("id");
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        DBUtil dbUtil = new DBUtil();
        // 1：加载驱动
//        Class.forName("com.mysql.cj.jdbc.Driver");
//        // 2：创建连接
//        String url = "jdbc:mysql://localhost:3306/medicald?serverTimezone=Asia/Shanghai";
//        String user = "root";
//        String password = "123456";
//        connection = DriverManager.getConnection(url, user, password);

        // 3：创建预编译命令对象
        // 假设表名为 patient，根据实际情况修改
        String sql = "INSERT INTO bingfang (patient_name, age, gender, birthday, diagnosis_result, admission_date, expected_discharge_date, ward_number, bed_number) " +
                "VALUES ('" + patient_name + "','" + age + "','" + gender + "','" + birthday + "','" + diagnosis_result + "','" + admission_date + "','" + expected_discharge_date + "','" + ward_number + "','" + bed_number + "');";
//        preparedStatement = connection.prepareStatement(sql);
//        preparedStatement.setString(1, patient_name);
//        preparedStatement.setString(2, age);
//        preparedStatement.setString(3, gender);
//        preparedStatement.setString(4, birthday);
//        preparedStatement.setString(5, diagnosis_result);
//        preparedStatement.setString(6, admission_date);
//        preparedStatement.setString(7, expected_discharge_date);
//        preparedStatement.setString(8, ward_number);
//        preparedStatement.setString(9, bed_number);

        // 4：执行命令对象，返回结果
        int i = dbUtil.executeUpdateBySQL(sql);
        if (i > 0) {
            response.sendRedirect("sickroom-addition.jsp");
        } else {
            out.println("插入失败");
        }
    } catch (Exception e) {
        e.printStackTrace();
        out.println("发生错误：" + e.getMessage());
    } finally {
        // 关闭资源
        try {
            if (preparedStatement != null) preparedStatement.close();
            if (connection != null) connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
%>


